package com.simple.space.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public class DateFormatUtil {
	
	static final ThreadLocal<SimpleDateFormat> TL_DATE_FORMAT = DateFormatUtil.threadLocalDateFormat("yyyyMMddHHmmss");
	
	public static long tick2time_t(final long tick){
		long time_t = 0L;			
		DateFormat df = TL_DATE_FORMAT.get();		
		try {
			Date date = df.parse(Long.toString(tick));
			if(null != date){							
				time_t = date.getTime()/1000L;
			}
		} catch (ParseException e) {
		}
		return time_t;
	}
	
	private static final String GMT_TIME_ZONE = "GMT+8:00".intern();
	
    public static ThreadLocal<SimpleDateFormat> threadLocalDateFormat(final String pattern) 
    {
        ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>() {
            protected SimpleDateFormat initialValue() {
                SimpleDateFormat df = new SimpleDateFormat(pattern, Locale.ENGLISH);
                df.setTimeZone(TimeZone.getTimeZone(DateFormatUtil.GMT_TIME_ZONE));
                return df;
            }
        };
        return tl;
    }
    public static ThreadLocal<SimpleDateFormat> threadLocalDateFormat
    (final String pattern, final TimeZone time_zone) 
    {
        ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>() 
        {
            protected SimpleDateFormat initialValue() {                             
                SimpleDateFormat df = new SimpleDateFormat(pattern, Locale.ENGLISH);
                df.setTimeZone(time_zone);
                return df;
            }
        };
        return tl;
    }
}
